macro(ax_depend)
  # confim the libs, prepare to link
  set(PLATFORM_SPECIFIC_LIBS)

  if(WINDOWS)
    list(APPEND PLATFORM_SPECIFIC_LIBS ws2_32 userenv psapi winmm Version Iphlpapi)

    if(WINRT)
      list(APPEND PLATFORM_SPECIFIC_LIBS windowscodecs Advapi32 runtimeobject Dwrite)
    elseif(AX_RENDER_API STREQUAL "gl" AND NOT AX_GLES_PROFILE) # ONLY Win32 Apps support DesktopGL if not use ANGLE
      list(APPEND PLATFORM_SPECIFIC_LIBS opengl32)
    endif()
  elseif(LINUX)
    # need review those libs: X11 Xi Xrandr Xxf86vm Xinerama Xcursor rt m
    list(APPEND PLATFORM_SPECIFIC_LIBS dl rt m fontconfig)

    # x11 libs
    list(APPEND PLATFORM_SPECIFIC_LIBS X11 Xi Xrandr Xxf86vm Xinerama Xcursor)

    if(_AX_HAVE_VLC)
      list(APPEND PLATFORM_SPECIFIC_LIBS vlc vlccore)
    endif()

    # use older cmake style on below linux libs
    ax_find_package(OpenGL OPENGL REQUIRED)
    set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
    find_package(Threads REQUIRED)
    set(THREADS_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  elseif(ANDROID)
    if(AX_RENDER_API STREQUAL "gl")
      # GLES backend
      list(APPEND PLATFORM_SPECIFIC_LIBS GLESv2 EGL log android OpenSLES)
    elseif(AX_RENDER_API STREQUAL "vk")
      # Vulkan backend
      list(APPEND PLATFORM_SPECIFIC_LIBS log android OpenSLES)
    endif()
  elseif(APPLE)
    include_directories(/System/Library/Frameworks)
    find_library(AUDIOTOOLBOX_LIBRARY AudioToolbox)
    find_library(FOUNDATION_LIBRARY Foundation)
    find_library(QUARTZCORE_LIBRARY QuartzCore)
    find_library(GAMECONTROLLER_LIBRARY GameController)
    find_library(METAL_LIBRARY Metal)
    find_library(COREVIDEO_LIBRARY CoreVideo)
    find_library(AVFOUNDATION_LIBRARY AVFoundation)
    find_library(COREMEDIA_LIBRARY CoreMedia)
    set(_AX_APPLE_LIBS
      ${AUDIOTOOLBOX_LIBRARY}
      ${QUARTZCORE_LIBRARY}
      ${FOUNDATION_LIBRARY}
      ${GAMECONTROLLER_LIBRARY}
      ${METAL_LIBRARY}
      ${COREVIDEO_LIBRARY}
      ${AVFOUNDATION_LIBRARY}
      ${COREMEDIA_LIBRARY}
    )

    if(MACOSX)
      list(APPEND PREBUILT_SPECIFIC_LIBS GLFW3)

      find_library(COCOA_LIBRARY Cocoa)
      find_library(APPLICATIONSERVICES_LIBRARY ApplicationServices)
      find_library(IOKIT_LIBRARY IOKit)
      find_library(APPKIT_LIBRARY AppKit)
      find_library(AUDIOUNIT_LIBRARY AudioUnit)
      find_library(COREAUDIO_LIBRARY CoreAudio)
      find_library(SYSTEMCONFIGURATION_LIBRARY SystemConfiguration)
      list(APPEND PLATFORM_SPECIFIC_LIBS
        ${COCOA_LIBRARY}
        ${APPLICATIONSERVICES_LIBRARY}
        ${IOKIT_LIBRARY}
        ${_AX_APPLE_LIBS}
        ${APPKIT_LIBRARY}
        ${AUDIOUNIT_LIBRARY}
        ${COREAUDIO_LIBRARY}
        ${SYSTEMCONFIGURATION_LIBRARY}
      )
      if(AX_RENDER_API STREQUAL "gl")
        find_library(OPENGL_LIBRARY OpenGL)
        list(APPEND PLATFORM_SPECIFIC_LIBS ${OPENGL_LIBRARY})
      endif()
    elseif(IOS)
      # Locate system libraries on iOS
      find_library(UIKIT_LIBRARY UIKit)
      find_library(AVKIT_LIBRARY AVKit)
      find_library(CORE_TEXT_LIBRARY CoreText)
      find_library(SECURITY_LIBRARY Security)
      find_library(CORE_GRAPHICS_LIBRARY CoreGraphics)

      if(NOT TVOS)
        find_library(CORE_MOTION_LIBRARY CoreMotion)
        find_library(WEBKIT_LIBRARY WebKit)
      endif()

      list(APPEND PLATFORM_SPECIFIC_LIBS
        ${UIKIT_LIBRARY}
        ${AVKIT_LIBRARY}
        ${CORE_TEXT_LIBRARY}
        ${SECURITY_LIBRARY}
        ${CORE_GRAPHICS_LIBRARY}
        ${AV_FOUNDATION_LIBRARY}
        ${_AX_APPLE_LIBS}
      )

      if(AX_RENDER_API STREQUAL "gl")
        find_library(OPENGLES_LIBRARY OpenGLES)
        list(APPEND PLATFORM_SPECIFIC_LIBS ${OPENGLES_LIBRARY})
      endif()

      if(NOT TVOS)
        list(APPEND PLATFORM_SPECIFIC_LIBS
          ${CORE_MOTION_LIBRARY}
          ${WEBKIT_LIBRARY}
        )
      endif()
    endif()
  elseif(WASM)
    if(AX_ENABLE_AUDIO AND NOT AX_USE_ALSOFT)
      list(APPEND PLATFORM_SPECIFIC_LIBS openal) # refer to: https://emscripten.org/docs/porting/Audio.html
    endif()
  endif()
endmacro()

macro(use_ax_depend target)
  ax_depend()

  foreach(platform_lib ${PLATFORM_SPECIFIC_LIBS})
    target_link_libraries(${target} ${platform_lib})
  endforeach()

  if(LINUX)
    ax_use_pkg(${target} OPENGL)
    ax_use_pkg(${target} THREADS)
  endif()
endmacro()
